/* Copyright (C) 2008  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/*
 * Node.java
 *
 * Created on October 25, 2007, 1:49 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.dei.perla.utils.emulator.model.node;


import org.dei.perla.utils.emulator.models.meta.Component;
import org.dei.perla.utils.emulator.models.net.NetworkCommunication;
import org.dei.perla.utils.emulator.models.world.Position;

/**
 *
 * @author Adnan
 */
public class Node implements Component {
    
    String id;
    String baseId;
    Position pos;
    AppSoft as;
    NetworkCommunication nc;
    RadioModel r;
    Battery b;
    SensorBoard sb;
    double redChargeAmount;
    
    NodeRadio nr;
    /** Creates a new instance of Node */
    public Node() {
        id = "1";       // default id is 1
        baseId = "-1";  // This is the base station 
        
        pos = new Position(0,0); // default position is the center
        as = new AppSoft();
        nc = new NetworkCommunication();
        r = new RadioModel();
        b = new Battery(10.0); // default battery charge is 10
        sb = new SensorBoard();
        
        redChargeAmount = 0.5;
        
        setReferenceOfAllComponent();
        startThreadToReduceCharge();
    }
    
    public Node(String id, String baseId, Position p, double charge) {
        this.id = id;       // default id is 1
        this.baseId = baseId;  // This is the base station 
        
        pos = p; // default position is the center
        as = new AppSoft();
        nc = new NetworkCommunication();
        r = new RadioModel();
        b = new Battery(charge); // default battery charge is 10
        sb = new SensorBoard();
      
        redChargeAmount = 0.5;
      
        setReferenceOfAllComponent();
        startThreadToReduceCharge();
    }
    
 void setReferenceOfAllComponent()
 {
    r.setReference(nc);
    r.setReference(b);
    nc.setReference(as);
    as.setReference(sb);
    sb.setReference(b);
 
 }
     
    public void setPosition(int x, int y)
    {
        pos.setPosition(new Position(x,y));
    }
    
    public Position getPosition()
    {
        return pos.getPosition();
    }
    
    public void sendData(String s)
    {
        nr = r;
        nr.sendData(s);
    }
    public String getData(String s)
    {
            nr = r;
        return nr.getData(s);  
     
    }
    
    public String getID() {return id;}
    
    public void displayData()
    {
        System.out.println("Node id: "+id);
        System.out.println("Base id: "+baseId);
        System.out.println("(x,y): " + pos.getX()+","+pos.getY());
        System.out.println("Charge: " + b.getCharge()) ;
     //   System.out.println("Temparature: "+ )
        
    }
    
    public String getCharge()
    {
        return (b.getCharge());
    }
    
    public void setCharge(double c)
    {
        b.setCharge(c);
    }
    
    public void startThreadToReduceCharge()
    {
        Runnable threadJob = new ReduceChargeThread();
        Thread myThread = new Thread(threadJob);
        
        myThread.start();
    
    }
    // Inner class that reduces the charge of the node after a certain time frequency
    class ReduceChargeThread implements Runnable
    {
        public void run(){
        
            try
            {
                while(true)
                {
                    Thread.sleep(5500);
                    doItNow();
                }
            }
            catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
        
        }
        
        public void doItNow(){
        
            double val = Double.parseDouble(getCharge());
            b.setCharge(val - redChargeAmount);
        
        }
    
    }
   
}
